﻿/*
 * @Author: Song Xinxin
 * @Date: 2022-10-30 17:46:44
 * @LastEditors: Song Xinxin
 * @LastEditTime: 2022-11-08 10:13:07
 * @FilePath: \WYW_0419_Oasis\JavaScripts\Modules\PlayerAttr\Module_PlayerAttr.ts
 * @Description: 玩家属性模块
 * 
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved. 
 */
import { DataCenterS, ModuleC, ModuleManager, ModuleS, SoundManager, UI } from "odin";
import { Consts } from "../../Consts";
import { UIGame } from "../../UI/UIGame";
import { ModuleC_Dress, ModuleS_Dress } from "../Dress/Module_Dress";
import { ModuleS_Sound } from "../Sound/Module_Sound";
import { Data_PlayerAttr } from "./Data_PlayerAttr";

//玩家属性模块服务端
export class ModuleS_PlayerAttr extends ModuleS<ModuleC_PlayerAttr,Data_PlayerAttr>
{

	Interval:number;

	/**
	 * 击中玩家
	 * @param player 开枪玩家
	 * @param hitplayer 被击中的玩家
	 */
	public hitPlayer(player:GamePlay.Player,hitplayer:GamePlay.Player):void
	{
		//判断被击中的玩家是否已经死亡和是否击中自己
		if((this.isDeath(hitplayer) == false)&&(player != hitplayer))
		{
			//被击中玩家掉血
			let kill = this.loseHP(hitplayer);
			//判断玩家是否被击杀
			if(kill == true)
			{
				this.kill(player);
			}
		}
	}

	/**
	 * 初始化玩家属性
	 */
	public initPlayerAttr():void
	{
		this.currentPlayer.character.switchToWalking();
		this.currentPlayer.character.isVisible = true;
		//玩家变成默认姿势
		this.currentPlayer.character.animationStance = GamePlay.AnimationStanceType.Empty;
		// 玩家朝向模式改为运动方向
		this.currentPlayer.character.moveFacingDirection = GamePlay.MoveFacingDirection.MovementDirection;

		this.currentData.setName(this.currentPlayer.character.characterName);
		this.currentData.setHP(100);
        this.currentData.setkill(0);
		this.currentData.setdeath(0);
		this.currentData.setcount(0);
		this.currentData.saveData(true);
	}


	/**
	 * 玩家掉血
	 * @param player 被攻击玩家
	 * @returns 是否被击杀
	 */
	public loseHP(player:GamePlay.Player):boolean
	{
		ModuleManager.instance.getModule(ModuleS_Sound).hitPlayerSound(player);
		let kill = false;
		let PlayerAttr = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
		//判断玩家受到攻击后是否死亡
		if(PlayerAttr.getHP > Consts.WeaponAttr.damage)
		{
			//玩家HP减少
			PlayerAttr.subHP(Consts.WeaponAttr.damage);
			//保存数据
			PlayerAttr.saveData(true);
		}
		else
		{
			//玩家死亡
			this.death(player);
			kill = true;
		}

		return kill;
	}

	/**
	 * 玩家死亡
	 * @param player 被击杀玩家
	 */
	public death(player:GamePlay.Player):void
	{
		let PlayerAttr = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
		//玩家HP设置为0
		PlayerAttr.setHP(0);
		//玩家死亡数加1
		PlayerAttr.adddeath(1);
		//启用布娃娃状态
		player.character.ragdoll(true);
		PlayerAttr.saveData(true);
		//显示死亡画面
		this.callClientFun(player,this.client.net_showDeath());
		//一定时间后复活
		this.Interval = setTimeout(()=>
		{
			this.rebirth(player);
			
		},Consts.RES_TIME*1000);
		
	}

	/**
	 * 玩家复活
	 * @param player 死亡玩家
	 */
	public rebirth(player:GamePlay.Player):void
	{
		let PlayerAttr = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
		//重置玩家HP
		PlayerAttr.setHP(100);
		//保存数据
		PlayerAttr.saveData(true);
		//在随机初生点位置复活
		let random = Math.round(Math.random()*5);
		player.character.location = Consts.BIRTH_POSITION[random];
		player.character.ragdoll(false);
		this.callClientFun(player,this.client.net_showRebirth());
	}


	/**
	 * 玩家击杀数增加
	 * @param player 击杀者
	 */
	public kill(player:GamePlay.Player):void
	{
		let PlayerAttr = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
		//玩家击杀数加1
		PlayerAttr.addkill(1);
		//保存数据
		PlayerAttr.saveData(true);
	}

	/**
	 * 判断被击中的玩家是否已经死亡
	 * @param player 被击中的玩家
	 * @returns 是否已经死亡
	 */
	public isDeath(player:GamePlay.Player):boolean
	{
		let PlayerAttr = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
		if(PlayerAttr.getHP > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}


}

//玩家属性模块客户端
export class ModuleC_PlayerAttr extends ModuleC<ModuleS_PlayerAttr,Data_PlayerAttr>
{
	/**
	 * 显示死亡UI
	 */
	public net_showDeath():void
	{
		UI.instance.getPanel(UIGame).Death();
	}

	/**
	 * 显示复活后UI
	 */
	public net_showRebirth():void
	{
		UI.instance.getPanel(UIGame).Rebirth();
	}

}	
